//
//  BIZPositionSkillCell.m
//  biz
//
//  Created by zhangfuwei on 2017/4/11.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZPositionSkillCell.h"
#import "BIZConsultorDetailModel.h"

static CGFloat kViewMargin = 15;
static CGFloat kEdgeMargin = 20;

@implementation BIZPositionSkillCell{
    
    UIImageView *_iconImgV;
    UILabel     *_titleLabel;
    UIButton    *_taxesButton;
    UIButton    *_tradeButton;
    UIButton    *_topicButton;
    
    UILabel     *_contentLabel;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self loadUI];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    [_iconImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(kViewMargin);
        make.top.equalTo(self.contentView).offset(kEdgeMargin);
        make.width.height.equalTo(@25);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_iconImgV.mas_right).offset(kViewMargin);
        make.top.equalTo(self.contentView).offset(kViewMargin);
        make.height.equalTo(@(kEdgeMargin));
    }];
    
    
    CGFloat margin = (Main_Screen_Width - 3*70)/4.0;
    [_tradeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel.mas_bottom).offset(kEdgeMargin);
        make.left.equalTo(self.contentView).offset(margin);
        make.height.equalTo(@25);
        make.width.equalTo(@70);
    }];
    
    [_taxesButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel.mas_bottom).offset(kEdgeMargin);
        make.left.equalTo(_tradeButton.mas_right).offset(margin);
        make.height.equalTo(@25);
        make.width.equalTo(@70);
    }];
    [_topicButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel.mas_bottom).offset(kEdgeMargin);
        make.left.equalTo(_taxesButton.mas_right).offset(margin);
        make.height.equalTo(@25);
        make.width.equalTo(@70);
    }];
    
    
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(kViewMargin);
        make.right.equalTo(self.contentView).offset(-1*kViewMargin);
        make.bottom.equalTo(self.contentView).offset(-1*kViewMargin);
        make.top.equalTo(_tradeButton.mas_bottom).offset(kEdgeMargin);
    }];
}

- (void)loadUI {
    
    _iconImgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"consultor_position"]];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.text = @"所在地区: 北京";

    _contentLabel = [[UILabel alloc] init];
    _contentLabel.numberOfLines = 0;

    _taxesButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _tradeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _topicButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_taxesButton setTitle:@"税种" forState:UIControlStateNormal];
    [_tradeButton setTitle:@"行业" forState:UIControlStateNormal];
    [_topicButton setTitle:@"专题" forState:UIControlStateNormal];
    _tradeButton.tag = 0;
    _taxesButton.tag = 1;
    _topicButton.tag = 2;
    [_tradeButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [_taxesButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [_topicButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    
//    [_tradeButton setBackgroundImage:[UIImage imageNamed:@"consultor_btnBg"] forState:UIControlStateNormal];
//    
//    [_tradeButton setTitleColor:COLOR_WORD_BLUE forState:UIControlStateNormal];
//    [_taxesButton setTitleColor:COLOR_WORD_BLACK forState:UIControlStateNormal];
//    [_topicButton setTitleColor:COLOR_WORD_BLACK forState:UIControlStateNormal];
    
    [self.contentView addSubview:_iconImgV];
    [self.contentView addSubview:_titleLabel];
    [self.contentView addSubview:_taxesButton];
    [self.contentView addSubview:_tradeButton];
    [self.contentView addSubview:_topicButton];
    [self.contentView addSubview:_contentLabel];
}

- (void)clickButton:(UIButton *)btn {
    
    [self switchLabelButtonsTitleWithTag:btn.tag];
    [self switchContentWithTag:btn.tag];
    !self.switchBlock ?: self.switchBlock(btn.tag);
}

-(void)setDetailM:(BIZConsultorDetailModel *)detailM {
    _detailM = detailM;
    [self switchLabelButtonsTitleWithTag:_selectedIdx];
    [self switchContentWithTag:_selectedIdx];
    _titleLabel.text = [NSString stringWithFormat:@"所在地区: %@",detailM.city];
    [self layoutIfNeeded];
}

- (void)switchContentWithTag:(NSInteger)idx {
    NSString *text ;
    switch (idx) {
        case 0:
            text = [_detailM.trade stringByReplacingOccurrencesOfString:@"," withString:@"  "];
            break;
        case 1:
            text = [_detailM.taxes stringByReplacingOccurrencesOfString:@"," withString:@"  "];
            break;
        case 2:
            text = [_detailM.topic stringByReplacingOccurrencesOfString:@"," withString:@"  "];
            break;
        default:
            break;
    }
    _contentLabel.text = text;
}

- (void)switchLabelButtonsTitleWithTag:(NSInteger )idx {
    
    UIImage *img = [UIImage imageNamed:@"consultor_btnBg"];
    [_tradeButton setBackgroundImage:idx==0?img:nil forState:UIControlStateNormal];
    [_taxesButton setBackgroundImage:idx==1?img:nil forState:UIControlStateNormal];
    [_topicButton setBackgroundImage:idx==2?img:nil forState:UIControlStateNormal];
    
    [_tradeButton setTitleColor:idx==0?COLOR_WORD_BLUE:COLOR_WORD_BLACK forState:UIControlStateNormal];
    [_taxesButton setTitleColor:idx==1?COLOR_WORD_BLUE:COLOR_WORD_BLACK forState:UIControlStateNormal];
    [_topicButton setTitleColor:idx==2?COLOR_WORD_BLUE:COLOR_WORD_BLACK forState:UIControlStateNormal];
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
